//
//  WBrefresfController.swift
//  weiboLQ
//
//  Created by 半缘君 on 15/12/14.
//  Copyright © 2015年 banyuanjun.cn. All rights reserved.
//

import UIKit
//旋转箭头指示器的临界点
let WBRefreshControlOffset:CGFloat = -60
class WBrefreshController: UIRefreshControl {
    /// 懒加载视图
    var refreshView = WBrefreshView.refresh()
    
    ///构造函数
    override init() {
        super.init()
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
    ///添加控件
    private func setupUI () {
        tintColor = UIColor.clearColor()
        addSubview(refreshView)
        //添加观察者
        self.addObserver(self, forKeyPath: "frame", options: [], context: nil)

        //刷新控件的约束
        refreshView.snp_makeConstraints { (make) -> Void in

            make.width.equalTo(120)
            make.height.equalTo(60)
            make.centerX.equalTo(self)
            make.centerY.equalTo(self)
        }
}
/// 开始刷新
    override func beginRefreshing() {
        super.beginRefreshing()
        refreshView.startAnimation()

    }
/// 结束刷新
    override func endRefreshing() {
        super.endRefreshing()
        refreshView.stopAnimation()
    }
    /// 监听刷新控件frame的方法
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        if frame.origin.y > 0 {
            return
        }
        
        if refreshing {
            refreshView.startAnimation()
            return
        }
        
        if frame.origin.y < WBRefreshControlOffset && !refreshView.rotateFlag {
            refreshView.rotateFlag = true
        } else if frame.origin.y > WBRefreshControlOffset && refreshView.rotateFlag {
            refreshView.rotateFlag = false
        }

    }
    //移除观察者
    deinit {
        self.removeObserver(self, forKeyPath: "frame")
    }

}
    
class WBrefreshView:UIView {
    /// 从XIb创建refreshView
    class func refresh () -> WBrefreshView {
        let rc = UINib(nibName: "WBrefresh", bundle: nil)
        return (rc.instantiateWithOwner(nil, options: nil)[0]) as! WBrefreshView
        
    }
    
    /// 旋转标记
    private var rotateFlag = false{
        didSet {
            rotateTipIcon()
        }
    }
    
    /// 旋转提示图标
    private func rotateTipIcon() {
        
        var angle = CGFloat(M_PI)
        angle += rotateFlag ? -0.00000001 : 0.00000001
        
        UIView.animateWithDuration(0.5) {
            self.downImage.transform = CGAffineTransformRotate(self.downImage.transform, angle)
        }
    }
    
    @IBOutlet weak var downImage: UIImageView!
    
    @IBOutlet weak var loadingIcon: UIImageView!
    
    @IBOutlet weak var tipView: UIView!
    
    /// 开始动画
    private func startAnimation() {
        
        let keyPath = "transform.rotation"
        if let _ = loadingIcon.layer.animationForKey(keyPath) {
            return
        }
        
        tipView.hidden = true
        
        let anim = CABasicAnimation(keyPath: keyPath)
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 0.5
        anim.removedOnCompletion = false
        
        loadingIcon.layer.addAnimation(anim, forKey: keyPath)
    }
    
    /// 停止动画
    private func stopAnimation() {
        tipView.hidden = false
        
        loadingIcon.layer.removeAllAnimations()
    }
    

}
